package com.ll.xiaonew.system.modules.job.task;

import com.alibaba.fastjson.JSONArray;
import com.ll.xiaonew.system.modules.job.dto.StatisticsInterfaceDto;
import com.ll.xiaonew.system.modules.job.entity.ScheduleJobInterfacecountEntity;
import com.ll.xiaonew.system.modules.job.service.StatisticsInterfaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * @author new
 * @date 2023/3/28 14:56
 */
@Component("StatisticsInterfaceForDayTask")
@Async // 使用异步处理任务
@EnableAsync // 开启异步处理
public class StatisticsInterfaceForDayTask implements ITask {

    @Autowired
    private StatisticsInterfaceService statisticsInterfaceService;


    /**
     * @author new
     * @Description: 统计每天接口调用次数，根据系统动态配置
     * @date 2023/3/28 16:34
     * @param
     * @return
     */
    @Override
    public void run(String params) {
        List<StatisticsInterfaceDto> list = statisticsInterfaceService.selectInterfaceCount();
        String str = JSONArray.toJSONString(list);

        //将查询出的数据set到实体类中
        ScheduleJobInterfacecountEntity scheduleJobInterfacecount = new ScheduleJobInterfacecountEntity();
        scheduleJobInterfacecount.setCountDetails(str);

        //获取昨日的日期
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,-24);
        scheduleJobInterfacecount.setCountTime(dateFormat.format(calendar.getTime()));

        scheduleJobInterfacecount.setStatus(0);

        statisticsInterfaceService.save(scheduleJobInterfacecount);

        System.out.println("定时任务执行了");

    }
}
